/* Copyright 2011 Rok Lenarčič 
 * 
 * This file is a part of MicroFn.
 * 
 * MicroFn is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * MicroFn is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/> */
package net.rlenar.microfn;

import net.rlenar.microfn.desc.FnName;

public class FnException extends RuntimeException {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Fn fn = null;
	private Object[] args = null;
	private String msg = "Exception occured in a fn";
	public FnException(Exception e) {
		super(e);
		initCause(e);
	}
	public FnException(Exception e, Fn function, Object[] args) {
		super(e);
		initCause(e);
		this.fn = function;
		this.args = args;
	}
	public FnException(String msg, Exception e, Fn function, Object[] args) {
		super(e);
		initCause(e);
		this.fn = function;
		this.args = args;
	}	
	public FnException(String msg, Fn function, Object[] args) {
		super();
		this.fn = function;
		this.args = args;
	}	
	@Override
	public String getMessage() {
		return msg;
	}
	@Override
	public String getLocalizedMessage() {
		return msg;
	}
	@Override
	public String toString() {
		String fnString = "";
		String argsString = "";
		if (fn != null) {
			fnString = "Fn: ";
			FnName anno = fn.getClass().getAnnotation(FnName.class);
			if (anno == null) {
				fnString += fn.getClass().getCanonicalName();
			} else {
				fnString += anno.value();
			}
		}
		if (args != null) {
			argsString = "Args: " + PrettyPrint.prettyPrint(args);
		}
		return msg + ":\n " + fnString + "\n" + argsString; 
	}
	public Fn getFn() {
		return fn;
	}
	public Object[] getArgs() {
		return args;
	}
	
}
